package main

// 定义一个区块链
func NewBlockChain() *BlockChain {
	//创建一个创世块, 并作为第一个区块链添加到区块链中
	gensisblock := GenesisBlock()
	return &BlockChain{
		blocks: []*Block{gensisblock},
	}
}

//创世块
func GenesisBlock() *Block {
	return NewBlock("创世块", []byte{})
}

//添加区块
func (bc *BlockChain) AddBlock(data string) {
	//获取前区块哈希
	lastBlock := bc.blocks[len(bc.blocks)-1]
	prevHash := lastBlock.Hash
	//创建新的区块
	block := NewBlock(data, prevHash)
	//添加到区块链数组中
	bc.blocks = append(bc.blocks, block)
}
